<?php

namespace app\admin\model;

use app\model\Model;

class Database extends Model
{
  public $privilege = 'database';
  public $dir;
  public $cids;
  public $dbc;

  function __construct()
  {
    include 'check.php';
    new Check();

    $this->dir = './' . SQL_DIR . '/';
    $this->cids = checkAdminPriv(@$this->privilege); // 判断权限
    include_once LIB_PATH . 'cls.database.php';

    $this->dbc = new \Database();
  }

  function index()
  {
    $this->tpl('database');
  }

  function table()
  {
    $this->tpl('database_table');
  }

  function databak()
  {
    $this->tpl('databak');
  }

  function backup()
  {
    $this->dbc->backup();
  }

  function restore()
  {
    $this->dbc->restore(str_safe($_GET['db']));
  }

  function repair()
  {
    $this->dbc->repair();
  }

  function optimize()
  {
    $this->dbc->optimize();
  }

  function init()
  {
    $this->dbc->init();
  }

  function del()
  {
    // $f = $this->dir . get_file_name($_GET['f']) . '.sql';
    if (file_delete($this->dir, get_file_name($_GET['f']) . '.sql')) {
      href(M('admin', 'database', 'databak'));
    }

    alert_href($GLOBALS['lang']['msg_tryagain'], M('admin', 'database', 'databak'));
  }

  function __destruct()
  {
  }
}